﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.ManagerService;
using Server.Models;
using CryptUtil;

namespace Server.ManagerManage
{
    public class ManagerBLL
    {
        ManagerDAL mDal = new ManagerDAL();
        /// <summary>
        /// 生存令牌
        /// </summary>
        /// <returns></returns>
        public string GetToken()
        {
            try
            {
                return Crypt.MD32Crypt(Guid.NewGuid().ToString(), DateTime.Now.Millisecond.ToString(), Encoding.UTF8);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 验证用户Token
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public bool CheckManager(string Token)
        {
            try
            {
                return mDal.CheckManager(Token);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 通过令牌获取客户信息
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public ManagerModel GetManagerByToken(string Token)
        {
            try
            {
                if (string.IsNullOrEmpty(Token))
                    throw new Exception("令牌为空!");
                return mDal.GetManagerByToken(Token);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="managerModel"></param>
        public ManageLoginModel ManagerLogin(ManagerModel managerModel)
        {
            try
            {
                if (null == managerModel)
                    throw new Exception("登录信息不能位空!");
                if (string.IsNullOrEmpty(managerModel.MLoginName))
                    throw new Exception("帐号不能为空!");
                if (string.IsNullOrEmpty(managerModel.MPassWord))
                    throw new Exception("密码不能为空!");
                //通过用户名获取用户信息
                ManagerModel manager = mDal.GetManagerByName(managerModel.MLoginName);
                if (null == manager)
                    throw new Exception("用户不存在!");
                //判断密码是否正确
                string pass = CryptUtil.Crypt.MD32Crypt(managerModel.MPassWord, manager.Sault, Encoding.UTF8);
                if (pass != manager.MPassWord)
                    throw new Exception("密码错误!");
                string token = this.GetToken();
                mDal.SaveOnline(token, manager.id);
                manager.MPassWord = "";
                manager.Sault = "";
                ManageLoginModel ml = new ManageLoginModel();
                ml.managerModel = manager;
                ml.Token = token;
                return ml;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 管理员退出
        /// </summary>
        /// <param name="Token"></param>
        public void ManagerLogout(string Token)
        {
            try
            {
                mDal.ManagerLogout(Token);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="manager"></param>
        public void ManagerAdd(ManagerModel manager)
        {
            try
            {
                manager.Sault = GetToken();
                manager.MPassWord = CryptUtil.Crypt.MD32Crypt(manager.MPassWord, manager.Sault, Encoding.UTF8);
                manager.State = 1;
                mDal.ManagerAdd(manager);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="manger"></param>
        public void PassEdit(ManagerModel manager,string OldPass)
        {
            try
            {
                if (string.IsNullOrEmpty(OldPass))
                    throw new Exception("原密码不能为空！");
                if (string.IsNullOrEmpty(manager.MPassWord))
                    throw new Exception("新密码不能为空！");
                ManagerModel ma = mDal.GetManagerByName(manager.MLoginName);
                if (null == manager)
                    throw new Exception("用户不存在!");
                //判断密码是否正确
                string pass = CryptUtil.Crypt.MD32Crypt(OldPass, ma.Sault, Encoding.UTF8);
                if (pass != ma.MPassWord)
                    throw new Exception("原密码错误!");
                manager.Sault = GetManagerByID(ma.id).Sault;
                manager.MPassWord = CryptUtil.Crypt.MD32Crypt(manager.MPassWord, manager.Sault, Encoding.UTF8);
                mDal.PassEdit(manager);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="manager"></param>
        public void ManagerEdit(ManagerModel manager)
        {
            try
            {
                mDal.ManagerEdit(manager);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        public void ManagerState(int id)
        {
            try
            {
                mDal.ManagerState(id);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerModel GetManagerByID(int id)
        {
            try
            {
                return null;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerModel[] GetManagerList()
        {
            try
            {
                return null;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
